layui.use(['layer', 'form', 'table'], function () {
    var layer = layui.layer,
        $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        t;                  //表格数据变量

    t = {
        elem: '#borrowTable',
        url: '/findBorrowBook',
        method: 'post',
        page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
            layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'], //自定义分页布局
            //,curr: 5 //设定初始在第 5 页
            groups: 6, //只显示 1 个连续页码
            first: "首页", //显示首页
            last: "尾页", //显示尾页
            limits: [3, 10, 20, 30]
        },
        width: $(parent.window).width() - 223,
        cols: [[
            {type: 'checkbox'},
            {field: 'bookName', title: '图书名称', width: '6%'},
            {field: 'bookCode', title: '图书编码', width: '6%'},
            {field: 'bookSite', title: '位置', width: '6%'},
            {field: 'bookAuthor', title: '作者', width: '6%'},
            {field: 'sname', title: '学生姓名', width: '6%'},
            {field: 'borrow', title: '审核状态',width: '6%',templet: '#borrowState'},
            {field: 'revertState', title: '归还状态',width: '6%',templet: '#revertState'},
            {field: 'reviewName', title: '审核人',width: '6%'},
            {field: 'reviewTime', title: '审核时间',width: '6%'},
            {field: 'borrowDate', title: '借阅时间', width: '6%'},
            {field: 'revertTime', title: '归还时间', width: '6%'},
            {field: 'revertDay', title: '归还天数', width: '6%'},
            {field: 'nickName', title: '创建人', width: '6%'},
            {field: 'borrowCount', title: '借阅数量',width: '6%'},
            {field: 'compenPrice', title: '赔偿金额',width: '6%'},
            {fixed: 'right', title: '操作', align: 'center',toolbar: '#borrowBar'}


        ]]
    };
    table.render(t);

    table.on('tool(borrowList)', function (obj) {
        var data = obj.data;
        if (obj.event === "checkBorrow") {
            layer.confirm("你确定要审核该记录么？", {btn: ['审核通过', '审核拒绝', '取消']}
                , function () {
                    $.post("/auditBorrow",{"borrowId": data.borrowId}, function (res) {
                        if (res.code === 0) {
                            layer.msg("操作成功", {time: 1000}, function () {
                                table.reload('borrowTable', t);
                            });
                        } else {
                            layer.msg(res.msg);
                        }
                    });
                }, function () {
                    $.post("/auditBorrowFail",{"borrowId": data.borrowId},function (res) {
                        if (res.code === 0) {
                            layer.msg("操作成功", {time: 1000}, function () {
                                table.reload('borrowTable', t);
                            });
                        } else {
                            layer.msg(res.msg);
                        }
                    });
                }, function (index) {
                    layer.close(index);
                }
            );
        }

        if (obj.event === 'revertBorrow') {
            var editIndex = layer.open({
                title: "赔偿价格",
                type: 2,
                content: "/bookBorrow/revertPrice?id=" + data.borrowId,
                success: function (layero, index) {
                    setTimeout(function () {
                        layer.tips('点击此处返回图书列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }, 500);
                }
            });
            //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
            $(window).resize(function () {
                layer.full(editIndex);
            });
            layer.full(editIndex);
        }
    });

        //功能按钮
        var active = {
            borrowBook: function () {
                var addIndex = layer.open({
                    title: "图书借阅",
                    type: 2,
                    content: "/borrow/add",
                    success: function (layero, addIndex) {
                        setTimeout(function () {
                            layer.tips('点击此处返回库存列表', '.layui-layer-setwin .layui-layer-close', {
                                tips: 3
                            });
                        }, 500);
                    }
                });
                //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
                $(window).resize(function () {
                    layer.full(addIndex);
                });
                layer.full(addIndex);
            },
        };

        $('.layui-inline .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //搜索
        form.on("submit(searchForm)", function (data) {
            t.where = data.field;
            table.reload('bookTable', t);
            return false;
        });

});
